package com.itic.mall.system.goods.goodsspu.service;

import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.applications.sys.syssetting.persistence.dao.SysSettingDao;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.type.DefaultSysSettingKeyValue;
import com.itic.appbase.framework.type.GoodsType;
import com.itic.appbase.framework.utils.HttpRequestUtils;
import com.itic.appbase.framework.utils.NumberStringHelper;
import com.itic.appbase.framework.utils.NumericHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.mobile.users.info.GoodsSpuVo;
import com.itic.mall.system.goods.goodscategory.service.GoodsCategoryService;
import com.itic.mall.system.goods.goodssku.service.GoodsSkuService;
import com.itic.mall.system.inter.goods.goodscategory.persistence.model.GoodsCategory;
import com.itic.mall.system.inter.goods.goodscategory.persistence.model.GoodsCategoryExample;
import com.itic.mall.system.inter.goods.goodssku.persistence.model.GoodsSku;
import com.itic.mall.system.inter.goods.goodsspu.persistence.dao.GoodsSpuDao;
import com.itic.mall.system.inter.goods.goodsspu.persistence.model.GoodsSpu;
import com.itic.mall.system.inter.goods.goodsspu.persistence.model.GoodsSpuExample;
import com.itic.mall.system.inter.users.openstore.persistence.dao.OpenStoreDao;
import com.itic.mall.system.inter.users.openstore.persistence.model.OpenStore;
import com.itic.mall.system.inter.users.openstore.persistence.model.OpenStoreExample;
import com.itic.mall.system.inter.users.openstore.persistence.model.OpenStoreExample.Criteria;
import com.itic.mall.system.ywym.goods.service.YwymGoodsService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class GoodsSpuService
     extends CrudService<GoodsSpuDao,GoodsSpu,GoodsSpuExample> {
    @Autowired
    private GoodsSpuDao goodsSpuDao;
    @Autowired
    private SysSettingDao sysSettingDao;
    @Autowired
    private OpenStoreDao openStoreDao;
    @Autowired
    private GoodsSkuService goodsSkuService;
    @Autowired
    private GoodsSpuService goodsSpuService;
    @Autowired
    private OutCommonService outCommonService;
    @Autowired
    private GoodsCategoryService goodsCategoryService;
    @Autowired
    YwymGoodsService ywymGoodsService;

	private static final String IS_SPECIAL = "T_DM_T_MARKET_SPECIAL_IS_SPECIAL";
	private static final String SPU_PLATFORM = "T_DM_T_MARKET_SPECIAL_SPU_PLATFORM";
	private static final String IS_TIME_OVER = "T_DM_T_MARKET_SPECIAL_IS_TIME_OVER";
	/**
	 * 根据id list查找商品列表
	 * @param ids
	 * @return
	 */
	public List<GoodsSpu> getGoodsListByIds(List<String> ids){
		List<GoodsSpu> list = null;
		if(ids.size()>0){
			GoodsSpuExample example = new GoodsSpuExample();
			com.itic.mall.system.inter.goods.goodsspu.persistence.model.GoodsSpuExample.Criteria criteria = example.createCriteria();
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);
			criteria.andIdIn(ids);
			list = this.goodsSpuDao.selectByExample(example);
		}
		return list;
	}
	/**
	 * 根据目录ID获取分类名称
	 * @param id
	 * @return
	 */
	public String getGoodsCategory(String id) {
		String name = null;
		//获取所有一级分类
				GoodsCategoryExample caexample = new GoodsCategoryExample();
				com.itic.mall.system.inter.goods.goodscategory.persistence.model.GoodsCategoryExample.Criteria criteriaca = caexample.createCriteria();
				criteriaca.andIsEffectiveEqualTo(DBConst.TRUE);
				criteriaca.andPidEqualTo("000");
				criteriaca.andIdEqualTo(id);
				List<GoodsCategory> list = goodsCategoryService.list(caexample);
				if(list.size()>0){
					name = list.get(0).getName();
				}
		return name;
	}
    public List<GoodsSpuVo> getGoodsSpuVoList(List<GoodsSpu> list){
		List<GoodsSpuVo> goodsList = new ArrayList<GoodsSpuVo>();
		if (list.size() > 0) {
			for(GoodsSpu goods:list){
				goods.setSpuPlatform(outCommonService.getNameByCode(SPU_PLATFORM, goods.getSpuPlatform()));
				String id = goods.getCategoryId();
				if(StringHelper.isNotBlank(id)){
					String name = getGoodsCategory(id);
					goods.setCategoryId(name);
				}
				GoodsSpuVo goodsSpuVo = new GoodsSpuVo();
				goodsSpuVo.setCategoryId(goods.getCategoryId());
				goodsSpuVo.setConPrice(goods.getConPrice());
				goodsSpuVo.setCouponMoney(goods.getCouponMoney());
				goodsSpuVo.setCreateTime(goods.getCreateTime());
				goodsSpuVo.setGoodsName(goods.getGoodsName());
				goodsSpuVo.setMainImageUrl(goods.getMainImageUrl());
				goodsSpuVo.setSpuId(goods.getId());
				goodsSpuVo.setSpuPlatform(goods.getSpuPlatform());
				goodsSpuVo.setWlCommission(goods.getWlCommission());
				goodsSpuVo.setWlPrice(goods.getWlPrice());
				Integer maxCount = goodsSkuService.getMaxCount(goods.getId());
				goodsSpuVo.setMaxCount(maxCount);
				goodsList.add(goodsSpuVo);
			}
		}
		return goodsList;
    }
    /**
     * 商品列表展示条件查询
     * @param sourceFlag 页面来源标志（special 特卖）
     * @param request
     * @param spuIds
     * @param shareId
     * @param goodsSpu
     * @param wlcReorder
     * @param wlcsReorder
     * @param timeReorder
     * @return
     */
    public GoodsSpuExample queryCondition(HttpServletRequest request,String userId,String sourceFlag,String spuIds, String shareId, GoodsSpu goodsSpu,String wlcReorder,String wlcsReorder,String timeReorder) {
		// 参数处理
		GoodsSpuExample example = new GoodsSpuExample();
		// 按创建时间倒序
		example.setOrderByClause("update_time desc");
		// 按排列号顺序
		example.setOrderByClause("reorder asc");
		//查营销活动下的商品IDS
		
		com.itic.mall.system.inter.goods.goodsspu.persistence.model.GoodsSpuExample.Criteria criteria = example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
		
		String categoryCodeId = request.getParameter("codeid");
		List<String> IDS = ywymGoodsService.getYwymGoodsList(userId);
		// 过滤该设备已设置一物一码展示商品
		if(IDS.size()>0){
			criteria.andIdNotIn(IDS);
		}
		if(StringHelper.isNotBlank(sourceFlag)){
			if("special".equals(sourceFlag)){
				//特卖页面过来的走下面逻辑，特卖可选商品只展示非特卖商品
				if(StringHelper.isBlank(spuIds)){
					criteria.andIsSpecialNotEqualTo(DBConst.TRUE);//查询非特卖商品
				}else{
					criteria.andIsSpecialEqualTo(DBConst.TRUE);
				}
			}
		}
		if(StringHelper.isNotBlank(spuIds)){
			spuIds = spuIds.replace("[", "");
			spuIds = spuIds.replace("]", "");
			spuIds = spuIds.replace(" ", "");
			List<String> ids = new ArrayList<String>();
			String[] goodsIds = spuIds.split(",");
			for(int i=0;i<goodsIds.length;i++){
				ids.add(goodsIds[i]);
			}
			if(ids.size()>1){
				criteria.andIdIn(ids);
			}
			if(ids.size() == 1){
				criteria.andIdEqualTo(ids.get(0));
			}
		}
		if (null !=  goodsSpu) {
			// 模糊查询
			if (StringHelper.isNotBlank(goodsSpu.getGoodsName())) {
				criteria.andGoodsNameLike("%" + goodsSpu.getGoodsName() + "%");
			}
			if (StringHelper.isNotBlank(categoryCodeId)) {
				criteria.andCategoryCodeidEqualTo(categoryCodeId);
			}
			if(StringHelper.isNotBlank(goodsSpu.getCategoryId())){
				criteria.andCategoryIdEqualTo(goodsSpu.getCategoryId());
			}
			if(StringHelper.isNotBlank(goodsSpu.getSpuPlatform())){
				criteria.andSpuPlatformEqualTo(goodsSpu.getSpuPlatform());
			}
			if(StringHelper.isNotBlank(goodsSpu.getIsJingping())){
				criteria.andIsJingpingEqualTo(goodsSpu.getIsJingping());
			}
			if(StringHelper.isNotBlank(goodsSpu.getIsHot())){
				criteria.andIsHotEqualTo(goodsSpu.getIsHot());
			}
			//按照佣金升序，降序排序
			/*String wlcReorder,String wlcsReorder,String timeReorder*/
			if(StringHelper.isNotBlank(wlcReorder)){
				if(wlcReorder.equals("001")){
					example.setOrderByClause("WL_COMMISSION asc");
				}
				if(wlcReorder.equals("002")){
					example.setOrderByClause("WL_COMMISSION desc");
				}
			}
			if(StringHelper.isNotBlank(wlcsReorder)){
				if(wlcsReorder.equals("001")){
					example.setOrderByClause("WL_COMMISSION_SHARE asc");
				}
				if(wlcsReorder.equals("002")){
					example.setOrderByClause("WL_COMMISSION_SHARE desc");
				}
			}		
			if(StringHelper.isNotBlank(timeReorder)){
				if(timeReorder.equals("001")){
					example.setOrderByClause("create_time asc");
				}
				if(timeReorder.equals("002")){
					example.setOrderByClause("create_time desc");
				}
			}
			
		}
		
		return example;
	}
    public List<GoodsSpu> getGoodsSpu(String specialId){
    	List<GoodsSpu> list = null;
    	if(StringHelper.isNotBlank(specialId)){
    		GoodsSpuExample example = new GoodsSpuExample();
    		com.itic.mall.system.inter.goods.goodsspu.persistence.model.GoodsSpuExample.Criteria criteria = example.createCriteria();
    		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    		criteria.andSpecialIdEqualTo(specialId);
    		list = this.goodsSpuDao.selectByExample(example);
    	}
    	return list;
    }
    /**
     * 根据特卖编号和商品编号查找商品信息
     * @param specialId
     * @param spuId
     * @return
     */
    public GoodsSpu getOneGoodsSpu(String specialId,String spuId){
    	GoodsSpu goodsSpu = null;
    	if(StringHelper.isNotBlank(spuId) && StringHelper.isNotBlank(specialId)){
    		GoodsSpuExample example = new GoodsSpuExample();
    		com.itic.mall.system.inter.goods.goodsspu.persistence.model.GoodsSpuExample.Criteria criteria = example.createCriteria();
    		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    		criteria.andSpecialIdEqualTo(specialId);
    		criteria.andIdEqualTo(spuId);
    		List<GoodsSpu> list = this.goodsSpuDao.selectByExample(example);
    		if(list.size()>0){
    			goodsSpu = list.get(0);
    		}
    	}
    	return goodsSpu;
    }
    /**
     * 修改特买啥商品信息
     * @param spuIds
     * @param couponMobeys
     * @param wlcommisoins
     * @return
     */
    public String updateGoodsSpu(List<String> spuIds,List<Double> couponMobeys,List<Double> wlcommisoins){
    	String result = ResponseCode.SERVER_ERROR.getCode();
		if (spuIds.size() > 0 && couponMobeys.size() > 0 && wlcommisoins.size() > 0
				&& couponMobeys.size() == wlcommisoins.size() && spuIds.size() == wlcommisoins.size()) {
			for (int i = 0; i < spuIds.size(); i++) {
				GoodsSpu goodsSpu = getGoodsSpuBySpuId(spuIds.get(i));
				if(couponMobeys.get(i) != null || wlcommisoins.get(i) != null){
					if(couponMobeys.get(i) != null){
						goodsSpu.setCouponMoney(couponMobeys.get(i));
					}
					if(wlcommisoins.get(i) != null){
						goodsSpu.setWlCommission(wlcommisoins.get(i));
					}
					int back = this.goodsSpuDao.updateByPrimaryKey(goodsSpu);
					if (back == 1) {
						result = ResponseCode.SUCCESS.getCode();
					}
				}else{
					result = ResponseCode.SUCCESS.getCode();
				}
			}
			}
		return result;
    }
    /**
     * 修改单个特卖商品
     * @param spuId
     * @param couponMoney
     * @return
     */
    public String updateOneGoodsSpu(String spuId,Double couponMoney){
    	String result = ResponseCode.SERVER_ERROR.getCode();
    	if(StringHelper.isNotBlank(spuId) && couponMoney != null){
    		GoodsSpu goodsSpu = getGoodsSpuBySpuId(spuId);
    		goodsSpu.setId(spuId);
    		goodsSpu.setCouponMoney(couponMoney);
    		int back = this.goodsSpuDao.updateByPrimaryKey(goodsSpu);
    		if(back == 1){
    			result = ResponseCode.SUCCESS.getCode();
    		}
    	}
    	return result;
    }
    public GoodsSpu getGoodsSpuBySpuId(String spuId){
    	GoodsSpu goodsSpu = null;
    	if(StringHelper.isNotBlank(spuId)){
    		GoodsSpuExample example = new GoodsSpuExample();
    		com.itic.mall.system.inter.goods.goodsspu.persistence.model.GoodsSpuExample.Criteria criteria = example.createCriteria();
    		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    		criteria.andIdEqualTo(spuId);
    		List<GoodsSpu> list = this.goodsSpuDao.selectByExample(example);
    		if(list.size()>0){
    			goodsSpu = list.get(0);
    		}
    	}
    	return goodsSpu;
    }
    public GoodsSpu getBySku(String skuId){
    	return goodsSpuDao.getBySku(skuId);
    }
    
    public GoodsSpu getBySpuNum(String spuNum){
    	return goodsSpuDao.getBySpuNum(spuNum);
    }
    
    public GoodsSpu getByskuId(String skuId){
    	return goodsSpuDao.getByskuId(skuId);
    }
	
	/**
	 * 根据SpuId 获取 SkuId
	 * @author 王文鑫
	 * @date 2018年1月25日 下午3:18:23
	 * @param skuId
	 * @return
	 */
	public String getSkuIdBySpuId(String spuId){
    	return goodsSpuDao.getSkuIdBySpuId(spuId);
    }
	

	public String getPoster(GoodsSpu goodsSpu, String historyId, String userId){
		String result = null;
		
		StringBuffer sb = new StringBuffer();
		//String url = "http://localhost:8090/image-server/uploadImage/oss/good";
		String url = "http://file.jhjvip.cn:8888/image-server/uploadImage/oss/poster";
		
		sb.append("mainImageUrl=");
		sb.append(goodsSpu.getMainImageUrl());
		sb.append("&redPackage=");
		sb.append(goodsSpu.getCouponMoney());
		sb.append("&preferentialPrice=");
		sb.append(goodsSpu.getConPrice());
		sb.append("&salesVolume=");
		sb.append(goodsSpu.getTotalSales());
		sb.append("&originalPrice=");
		sb.append(goodsSpu.getPrice());
		sb.append("&productName=");
		sb.append(goodsSpu.getGoodsName());
		sb.append("&type=");
		sb.append(goodsSpu.getSpuPlatform());
		
		String initUrl = "http://wap.uhuijia.com.cn/index.html#/loading?id=";
		String temp = sysSettingDao.getByKey("GZH_SHARE_FILTER_URL");
		if( StringHelper.isNotBlank(temp) ){
			initUrl = temp;
		}
		sb.append("&url="+initUrl);
		sb.append(historyId);
		String sendPost = HttpRequestUtils.sendPost(url, sb.toString());
		JSONObject fromObject = JSONObject.fromObject(sendPost);
		String resultRemote = fromObject.getString("errorCode");
		if( "10000".equals(resultRemote)){
			result = fromObject.getString("data");
		}
		return result;
		
	}

	public String getCommonErheyiLink(String spuId, String userId) {
		String erHeYiLink = "";
		GoodsSpu spu = goodsSpuDao.selectByPrimaryKey(spuId);
		if( null != spu && "jd".equals(spu.getSpuPlatform()) ){
			String position = "";
			String skuNum = "";
			skuNum = spu.getSkuId();
			position = getJdPositionByUserId(userId);
			erHeYiLink = getCommonLinkByDdt(skuNum,position);
		}else{
			/*erHeYiLink = "http://down.huifakeji.com/dist/index.html#/goodsDetail?type=1&goodsId="+spuId+"&yunId="+userId;
			String tempLink = sysSettingDao.getByKey("GZH_SHARE_GOODS_DETAIL_URL");
			if( StringHelper.isNotBlank(tempLink) ){
				erHeYiLink = tempLink+spuId+"&yunId="+userId;
			}*/
			erHeYiLink = DefaultSysSettingKeyValue.spuDetailLink.getValue()+spuId+"&yunId="+userId;
			String sysWord = sysSettingDao.getByKey(DefaultSysSettingKeyValue.spuDetailLink.getValue());
			if( StringHelper.isNotBlank(sysWord) ){
				erHeYiLink = sysWord+spuId+"&yunId="+userId;
			}
			
		}
		//生成推广位链接
		return erHeYiLink;
	}

	/**
	 * 根据用户ID获得用户推广位id
	 * @param userId
	 * @return
	 */
	public String getJdPositionByUserId( String userId ){
		String position = "";
		OpenStoreExample example = new OpenStoreExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andUserIdEqualTo(userId);
		criteria.andTypeEqualTo(GoodsType.jd.getState());
		criteria.andStatusEqualTo(DBConst.TRUE);
		List<OpenStore> list = openStoreDao.selectByExample(example);
		if( list.size()>0 ){
			OpenStore store = list.get(0);
			position = store.getTgwId();
		}
		return position;
	}
	
	public String getConErheyiLink(String spuId, String userId) throws Exception {
		String erHeYiLink = "";
		GoodsSpu spu = goodsSpuDao.selectByPrimaryKey(spuId);
		//生成推广位链接
		if( null != spu && "jd".equals(spu.getSpuPlatform()) ){
			String position = "";
			String skuNum = "";
			String conUrl  = "";
			skuNum = spu.getSkuId();
			conUrl = spu.getConUrl();
			position = getJdPositionByUserId(userId);
			erHeYiLink = getConLinkByDdt(skuNum,conUrl,position);
		}else{
			/*erHeYiLink = "http://down.huifakeji.com/dist/index.html#/redpackage?type=1&goodsId="+spuId+"&yunId="+userId;
			String tempLink = sysSettingDao.getByKey("GZH_SHARE_REDPACKAGE_URL");
			if( StringHelper.isNotBlank(tempLink) ){
				erHeYiLink = tempLink+spuId+"&yunId="+userId;
			}*/
			erHeYiLink = DefaultSysSettingKeyValue.packageLink.getValue()+spuId+"&yunId="+userId;
			String sysWord = sysSettingDao.getByKey(DefaultSysSettingKeyValue.packageLink.getValue());
			if( StringHelper.isNotBlank(sysWord) ){
				erHeYiLink = sysWord+spuId+"&yunId="+userId;
			}
		}
		return erHeYiLink;
	}
	
	/**
	 * 获得京东二合一接口
	 * @param skuNum
	 * @param position
	 * @return
	 */
	public String getCommonLinkByDdt( String skuNum, String position ){
		String result = "";
		String initUrl = "http://develop.jhjvip.cn:8081/ddt/mobile/jdlm/getUrl?sku=";
		String byKey = sysSettingDao.getByKey("DDT_WWW_ADDRESS");
		if( StringHelper.isNotBlank(byKey) ){
			initUrl = byKey;
		}
		initUrl = initUrl + skuNum;
		
		if( StringHelper.isNotBlank(position) ){
			initUrl = initUrl+"&pid="+position;
		}
		String httpGet = HttpRequestUtils.httpGet(initUrl);
		JSONObject fromObject = JSONObject.fromObject(httpGet);
		String resultHttp = fromObject.getString("code");
		if( "10000".equals(resultHttp) ){
			result = fromObject.getString("data");
		}
		return result;
	}
	
	/**
	 * 获得京东优惠券接口
	 * @param skuNum
	 * @param conUrl
	 * @param position
	 * @return
	 */
	public String getConLinkByDdt( String skuNum, String conUrl, String position )throws Exception{
		//skuNum = "21677689636";
		//conUrl = "http://coupon.m.jd.com/coupons/show.action?key=faa8d83af02c41aabdf1823c95264179&roleId=9547553&to=mall.jd.com/index-186091.html";
		String result = null;
		String initUrl = "http://develop.jhjvip.cn:8081/ddt/mobile/jdlm/getConUrl";
		//String initUrl = "http://localhost:8090/ddt/mobile/jdlm/getConUrl";
		String byKey = sysSettingDao.getByKey("DDT_WWW_ADDRESS");
		if( StringHelper.isNotBlank(byKey) ){
			initUrl = byKey;
		}
		conUrl = URLEncoder.encode(conUrl,"utf-8");
		String param = "sku=" + skuNum +"&conUrl="+conUrl;
		if( StringHelper.isNotBlank(position) ){
			param += "&pid="+position;
		}
		String httpGet = HttpRequestUtils.sendPost(initUrl,param);
		JSONObject fromObject = JSONObject.fromObject(httpGet);
		String resultHttp = fromObject.getString("code");
		if( "10000".equals(resultHttp) ){
			result = fromObject.getString("data");
		}
		return result;
	}

	public void deleteById(String spuId) {
		goodsSpuDao.deleteByPrimaryKey(spuId);
	}
	
	
	/**
	 * 形成订单时支付后商品库存减少或增加 ,参数:商品号，数量，计算
	 * @author 王文鑫
	 * @date 2018年2月9日 上午10:03:55
	 * @param request
	 * @param response
	 * @param SkuId
	 * @param compute
	 * @param num
	 * @return
	 */
	public Map<String, Object> getCouponNumById(String SkuId,String compute,int num) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String result = ResponseCode.SERVER_ERROR.getCode();
		
		int spuNum = 0; //spu参数 
		int skuNum = 0; //sku参数
		
		int SpuTotalInventorys = 0; //spu修改后的参数
		int SkuTotalInventorys = 0; //sku修改后的参数
		
		GoodsSpu goodsSpu = new GoodsSpu();
		GoodsSku goodsSku = new GoodsSku();
		
		//判断参数
		if(StringHelper.isNotBlank(SkuId) && StringHelper.isNotBlank(compute) &&  num > 0 ){
			 goodsSpu = goodsSpuService.get(SkuId); //根据skuId获取sku商品
			 goodsSku = goodsSkuService.get(SkuId); //根据skuId 获取spu商品
			 //判断对象
			 if(null != goodsSpu && null != goodsSku){
				 
				 skuNum = goodsSku.getTotalInventory();
				 spuNum = goodsSpu.getTotalInventorys();
				 
				 if("sub".equals(compute)){
					//判断商品当前属性的库存
					if(spuNum != 0 && skuNum != 0){
						//sku库存是否大于需要修改的参数
						if(NumberStringHelper.isGreaterInteger(spuNum, num) && NumberStringHelper.isGreaterInteger(skuNum, num)){
							SpuTotalInventorys = NumericHelper.subInteger(spuNum, num);//减法
							SkuTotalInventorys = NumericHelper.subInteger(skuNum, num);
							
							goodsSpu.setTotalInventorys(SpuTotalInventorys);//重新设置spu库存属性
							goodsSku.setTotalInventory(SkuTotalInventorys);//重新设置sku库存属性
							
							result = goodsSpuService.edit(goodsSpu);//修改spu信息
							result = goodsSkuService.edit(goodsSku);//修改sku信息
						}else{
							result = "商品库存信息错误";//商品库存信息错误
						}
					}else{
						result = "商品库存信息为空";//商品库存信息为空
					}
					
				 }else if("add".equals(compute)){
					 
					SpuTotalInventorys = NumericHelper.addInteger(spuNum, num);//加法
				 	SkuTotalInventorys = NumericHelper.addInteger(skuNum, num);
					
					goodsSpu.setTotalInventorys(SpuTotalInventorys);//重新设置spu库存属性
					goodsSku.setTotalInventory(SkuTotalInventorys);//重新设置sku库存属性
					
					result = goodsSpuService.edit(goodsSpu);//修改spu信息
					result = goodsSkuService.edit(goodsSku);//修改sku信息
				 }
				
			 }else{
				 result = "获取对象错误";//获取对象错误
			 }
		}else{
			result = "缺少参数";//缺少参数
		}
		resultMap.put(IticConst.RESULT, result);
		return resultMap;
	}
	
	/**
	 * 商品换货 
	 * @author 王文鑫
	 * @date 2018年2月9日 上午10:09:11
	 * @param request
	 * @param response
	 * @param AddSkuId
	 * @param SubSkuId
	 * @param num
	 * @return
	 */
	public Map<String, Object> GoodsBarter(HttpServletRequest request, HttpServletResponse response,String AddSkuId,String SubSkuId,int num) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String result = ResponseCode.SERVER_ERROR.getCode();
		
		int skuAddNum = 0; //sku加法参数
		int skuSubNum = 0; //sku减法参数
		
		int addGoodsSkuInventorys = 0; //增加sku参数
		int subGoodsSkuInventorys = 0; //减少sku参数
		
		GoodsSku subGoodsSku = new GoodsSku(); //减少商品
		GoodsSku addGoodsSku = new GoodsSku(); //增加商品
		
		if(StringHelper.isNotBlank(AddSkuId) && StringHelper.isNotBlank(SubSkuId) &&  num > 0 ){
			 subGoodsSku = goodsSkuService.get(SubSkuId); //减少商品
			 addGoodsSku = goodsSkuService.get(AddSkuId); //增加商品
			 
			//判断对象
			 if(null != subGoodsSku && null != addGoodsSku){
				 skuAddNum = addGoodsSku.getTotalInventory();
				 skuSubNum = subGoodsSku.getTotalInventory();
				 
				 if(skuSubNum != 0){
					//sku库存是否大于需要修改的参数
						if(NumberStringHelper.isGreaterInteger(skuSubNum, num)){
							addGoodsSkuInventorys = NumericHelper.addInteger(skuAddNum, num);
							subGoodsSkuInventorys = NumericHelper.subInteger(skuSubNum, num);
							
							addGoodsSku.setTotalInventory(addGoodsSkuInventorys);//重新设置spu库存属性
							subGoodsSku.setTotalInventory(subGoodsSkuInventorys);//重新设置sku库存属性
							
							result = goodsSkuService.edit(addGoodsSku);//修改spu信息
							result = goodsSkuService.edit(subGoodsSku);//修改sku信息
						}else{
							result = "商品库存信息错误";//商品库存信息错误
						}
				 }else{
					 result = "商品库存信息为空";//商品库存信息为空
				 }
			 }else{
				 result = "获取对象错误";//获取对象错误
			 }
		}else{
			result = "缺少参数";//缺少参数
		}
		resultMap.put(IticConst.RESULT, result);
		return resultMap;
	}
	
}